package model

import (
    "fmt"
    "oauth2/config"
    "oauth2/pkg/ldap"
)

type User struct {
    ID int `gorm:"primary_key" json:"id"`
    Name string `json:"name"`
    Password string `json:"password"`
}

func (u *User) TableName() string {
    return "user"
}

func (u *User) GetUserIDByPwd(username, password string) (userID string) {
    println("GetUserIDByPwd-------------")
    // use the db conn 
    // write your own user authentication logic
    // like:
    // db.Where("name = ? AND password = ?", username, password).First(u)
    // userID = u.ID
    if config.Get().Ldap.Enabled {
        err := ldap.UserAuthentication(username,password)
        if err != nil {
           fmt.Println("login with error ",err.Error())
           return
        }
        userID = username
        return
    }
    // test account: admin admin
    if username == "admin" && password == "admin" {
        userID = "admin"
    }

    return
}